<?php 
/**
 * 车辆管理类
 * @Author    紫梦
 * @DateTime  2017-11-03
 * @copyright 响响租车
 * @version   1.0
 * @UpdateTime 2017-11-10
 */
namespace app\index\controller;
use \think\Request;
use \think\Db;
use app\common\model\OffAbnormal;
use app\common\model\OffNormal;
use app\common\model\OffRecord;
use think\Exception;
use think\Log;
use app\common\enums\ResultEnum;
use app\common\utils\ResultVOUtil;
use app\common\controller\Execl;
use app\common\model\Agency;
use app\common\model\CarsInfo;
use app\common\model\Cars;
use app\common\model\Brand;
class Off extends Base
{
	public function abnormal(){
		$request = Request::instance();
        
        if($request->isAjax()){
            $post = $request->post();

            if( !empty($post['datemin']) && !empty($post['datemax']) ){
                $where['create_time'] = ['between time',[$post['datemin'],$post['datemax']]];                  
                    
            }
            if ( empty($post['datemin']) && !empty($post['datemax']) ){             
                $where['create_time'] = ['<= time',$post['datemax']];
                    
                    
            }
            if ( !empty($post['datemin']) && empty($post['datemax']) ) {
                $where['create_time'] = ['> time',$post['datemin']];
                 
            }

            if ( !empty($post['agency_id']) ) {
                $where['agency_id'] = $post['agency_id'];
                    
            }
            if ( !empty($post['brand_id']) ) {
                $where['brand_id'] = $post['brand_id'];
                    
            }
            if ( !empty($post['supplier_no']) ) {
                $where['supplier_no'] = $post['supplier_no'];
                    
            }

            if ( !empty($post['search_inpute']) ) {
                $where['car_sn|car_unique_id|cardcode|equipment_id|sim_id'] = ['like','%'.$post['search_inpute'].'%'];
                    
            }
            
            if( empty($where) ){
                $where = '1=1';
            }   

            $model= OffAbnormal::with('brand,agency')->where($where)->order('id', 'asc')
                            ->paginate($post['length'],false,[
                                    'type'     => 'bootstrap',
                                    'var_page' => 'page',
                                    'page'=>$post['start']/$post['length'] +1 ,
                                ]);
            if(count($model)>=1){
                foreach ($model as $key => $value) {
                    $res['data'][] = $value;
                }
                
                $res['recordsTotal'] = $res['recordsFiltered'] = $model->total();
            }else{
                $res['data'] = '';
                $res['recordsTotal'] = $res['recordsFiltered'] =0;
            }

           
            return $res;

        }else{
            $brand = Brand::all();
            $agency = Agency::all();
            unset($agency[0]);
            $this->assign('agency',$agency);
            $this->assign('brand',$brand);
            return $this->fetch();
        }
        
	}

	public function edit($id){
        $request = Request::instance();
        if($request->isAjax()){
            $param = $request->param();
            $cars = OffAbnormal::get($param['id']);
            $type_values = ["","小电瓶亏电","长租拆设备","维修","整备待上架","违章下架","自测试用","门店展车"];
            $data['car_sn'] = $cars['car_sn'];
            $data['car_unique_id'] = $cars['car_unique_id'];
            $data['brand_id'] = $cars['brand_id'];
            $data['agency_id'] = $cars['agency_id'];
            $data['sim_id'] = $cars['sim_id'];
            $data['cardcode'] = $cars['cardcode'];
            $data['equipment_id'] = $cars['equipment_id'];
            $data['supplier_no'] = $cars['supplier_no'];
            $data['machine_report_time'] = $cars['machine_report_time'];
            $data['time'] = $param['time']*24*3600;
            $data['type'] = $param['type'];
            if($param['type'] == 8){

                $data['reason'] = $param['reason'];
            }else{
                $data['reason'] = $type_values[$param['type']];
            }
            $model = new OffRecord();
            try{

                $res = $model->allowField(true)->save($data);

            }catch(\Exception $e){
                Log::record('off_record表插入数据'.$e->getMessage(),'error');
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
            if($res){
                OffAbnormal::destroy($param['id']);    
                return ResultVOUtil::success('');
            }
            
        }else{
            $data = OffAbnormal::get($id);
            $datum = Db::name('off_record')->where(['car_unique_id'=>$data['car_unique_id']])->order('create_time desc')->find();
            $this->assign('data',$data);
            $this->assign('datum',$datum);
            return $this->fetch();
        }
	}

    public function normal(){
        $request = Request::instance();
        
        if($request->isAjax()){
            $post = $request->post();

            if( !empty($post['datemin']) && !empty($post['datemax']) ){
                $where['create_time'] = ['between time',[$post['datemin'],$post['datemax']]];                  
                    
            }
            if ( empty($post['datemin']) && !empty($post['datemax']) ){             
                $where['create_time'] = ['<= time',$post['datemax']];
                    
                    
            }
            if ( !empty($post['datemin']) && empty($post['datemax']) ) {
                $where['create_time'] = ['> time',$post['datemin']];
                 
            }

            if ( !empty($post['agency_id']) ) {
                $where['agency_id'] = $post['agency_id'];
                    
            }
            if ( !empty($post['brand_id']) ) {
                $where['brand_id'] = $post['brand_id'];
                    
            }
            if ( !empty($post['supplier_no']) ) {
                $where['supplier_no'] = $post['supplier_no'];
                    
            }

            if ( !empty($post['type']) ) {
                $where['type'] = $post['type'];
                    
            }

            if ( !empty($post['search_inpute']) ) {
                $where['car_sn|car_unique_id|cardcode|equipment_id|sim_id'] = ['like','%'.$post['search_inpute'].'%'];
                    
            }
            
            if( empty($where) ){
                $where = '1=1';
            }   

            $model= OffNormal::with('brand,agency')->where($where)->order('id', 'asc')
                            ->paginate($post['length'],false,[
                                    'type'     => 'bootstrap',
                                    'var_page' => 'page',
                                    'page'=>$post['start']/$post['length'] +1 ,
                                ]);
            if(count($model)>=1){
                foreach ($model as $key => $value) {
                    $res['data'][] = $value;
                }
                
                $res['recordsTotal'] = $res['recordsFiltered'] = $model->total();
            }else{
                $res['data'] = '';
                $res['recordsTotal'] = $res['recordsFiltered'] =0;
            }

           
            return $res;

        }else{
            $brand = Brand::all();
            $agency = Agency::all();
            unset($agency[0]);
            $this->assign('agency',$agency);
            $this->assign('brand',$brand);
            return $this->fetch();
        }
        
    }

    public function record(){
        $request = Request::instance();
        
        if($request->isAjax()){
            $post = $request->post();

            if( !empty($post['datemin']) && !empty($post['datemax']) ){
                $where['create_time'] = ['between time',[$post['datemin'],$post['datemax']]];                  
                    
            }
            if ( empty($post['datemin']) && !empty($post['datemax']) ){             
                $where['create_time'] = ['<= time',$post['datemax']];
                    
                    
            }
            if ( !empty($post['datemin']) && empty($post['datemax']) ) {
                $where['create_time'] = ['> time',$post['datemin']];
                 
            }
            if ( !empty($post['search_inpute']) ) {
                $where['car_sn|car_unique_id|cardcode|equipment_id|sim_id'] = ['like','%'.$post['search_inpute'].'%'];
                    
            }
            if ( !empty($post['type']) ) {
                $where['type'] = $post['type'];
                    
            }
            if ( !empty($post['supplier_no']) ) {
                $where['supplier_no'] = $post['supplier_no'];
                    
            }
            
            if( empty($where) ){
                $where = '1=1';
            }       
            $model= OffRecord::with('brand,agency')->where($where)->order('create_time', 'desc')
                            ->paginate($post['length'],false,[
                                    'type'     => 'bootstrap',
                                    'var_page' => 'page',
                                    'page'=>$post['start']/$post['length'] +1 ,
                                ]);
            if(count($model)>=1){
                foreach ($model as $key => $value) {
                    $res['data'][] = $value;
                }
                
                $res['recordsTotal'] = $res['recordsFiltered'] = $model->total();
            }else{
                $res['data'] = '';
                $res['recordsTotal'] = $res['recordsFiltered'] =0;
            }

           
            return $res;

        }else{
            $brand = Brand::all();
            $agency = Agency::all();
            unset($agency[0]);
            $this->assign('agency',$agency);
            $this->assign('brand',$brand);
            return $this->fetch();
        }
        
    }

    public function excelToData(){
        $request = Request::instance();
        if($request->isPost()){
            $file = $request->file('excel');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    $url = $info->getSaveName();
                    $ext = $info->getExtension();
                    $path = ROOT_PATH . 'public' . DS . 'uploads/'.$url;
                    $excel = new Execl;
                    $excelData = $excel->readerBase($path);
                    if(empty($excelData)){
                        $this->error('数据格式不合法');
                    }
                    $insertNum = 0;
                    // 遍历数据加入到数据库中去 
                    foreach ($excelData as $key => $value) {
                        if($key == 0){
                            continue;
                        }
                        if($value[5]!==null){      
                            // 查看库中是否有数据
                            $off_cars = Db::name('off_record')->where(['car_unique_id'=>$value[1]])->find();
                            $cars = Db::name('off_write')->where(['car_unique_id'=>$value[1]])->find();
                            $data['car_sn'] = $cars['car_sn'];
                            $data['car_unique_id'] = $cars['car_unique_id'];
                            $data['brand_id'] = $cars['brand_id'];
                            $data['agency_id'] = $cars['agency_id'];
                            $data['sim_id'] = $cars['sim_id'];
                            $data['cardcode'] = $cars['cardcode'];
                            $data['equipment_id'] = $cars['equipment_id'];
                            $data['supplier_no'] = $cars['supplier_no'];
                            $data['machine_report_time'] = $cars['machine_report_time'];
                            $data['reason'] = $value[5]??'';
                            $model = new OffRecord;

                            if(empty($off_cars)){
                                try{

                                    $res = $model->allowField(true)->save($data);
                                    $insertNum++;

                                }catch(\Exception $e){
                                    Log::record('off_record表插入数据'.$e->getMessage(),'error');
                                    
                                }
                                if($res){
                                    OffWrite::destroy(['car_unique_id'=>$data['car_unique_id']]);      
                                }
                            }  
                        }
                    }

                    $resArr = ['status'=>0,'msg' =>'插入'.$insertNum.'条'];
                    return $resArr;

                }else{
                    
                    $this->error($file->getError());
                }
                
            }else{
                $this->error('没有上传文件');
            }

        }else{
            return $this->fetch('excel_to_data');
        }
    }
}